@echo off
chcp 65001 >nul
REM setup-mcp.bat - MCP 服务自动配置脚本
REM 版本: 1.0.0
REM 用途: 在 Windows 11 新电脑上自动配置 MCP 服务

echo === MCP 服务配置脚本 ===
echo 版本: 1.0.0
echo.

REM 1. 检查 Node.js
echo [1/6] 检查 Node.js...
node --version >nul 2>&1
if errorlevel 1 (
    echo ❌ Node.js 未安装，请先安装: https://nodejs.org/
    echo    推荐安装 LTS 版本 ^(^>= 20.10.0^)
    pause
    exit /b 1
)
for /f "tokens=*" %%i in ('node --version') do set NODE_VERSION=%%i
echo ✓ Node.js 版本: %NODE_VERSION%

REM 2. 检查 npm
echo.
echo [2/6] 检查 npm...
npm --version >nul 2>&1
if errorlevel 1 (
    echo ❌ npm 不可用
    pause
    exit /b 1
)
for /f "tokens=*" %%i in ('npm --version') do set NPM_VERSION=%%i
echo ✓ npm 版本: %NPM_VERSION%

REM 3. 安装 Playwright
echo.
echo [3/6] 安装 Playwright...
echo    这可能需要几分钟时间，请耐心等待...
call npm install -g @playwright/test >nul 2>&1
if errorlevel 1 (
    echo ⚠ Playwright 安装失败，但不影响其他服务
    echo    可以稍后手动安装: npm install -g @playwright/test
) else (
    call npx playwright install chromium >nul 2>&1
    echo ✓ Playwright 安装完成
)

REM 4. 创建配置目录
echo.
echo [4/6] 创建配置目录...
if not exist "%USERPROFILE%\.claude" (
    mkdir "%USERPROFILE%\.claude" 2>nul
    if errorlevel 1 (
        echo ❌ 创建目录失败
        pause
        exit /b 1
    )
    echo ✓ 创建目录: %USERPROFILE%\.claude
) else (
    echo ✓ 目录已存在: %USERPROFILE%\.claude
)

REM 5. 备份现有配置
echo.
echo [5/6] 备份现有配置...
if exist "%USERPROFILE%\.claude\claude_desktop_config.json" (
    set TIMESTAMP=%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%
    set TIMESTAMP=%TIMESTAMP: =0%
    copy "%USERPROFILE%\.claude\claude_desktop_config.json" "%USERPROFILE%\.claude\claude_desktop_config.json.backup.%TIMESTAMP%" >nul 2>&1
    if errorlevel 1 (
        echo ⚠ 备份失败，将继续写入新配置
    ) else (
        echo ✓ 已备份现有配置
    )
) else (
    echo ✓ 无需备份（配置文件不存在）
)

REM 6. 写入配置
echo.
echo [6/6] 写入 MCP 配置...
(
echo {
echo   "mcpServers": {
echo     "context7": {
echo       "command": "npx",
echo       "args": ["-y", "@upshift-inc/context7"]
echo     },
echo     "serena": {
echo       "command": "npx",
echo       "args": ["-y", "@upshift-inc/serena"]
echo     },
echo     "skill-seeker": {
echo       "command": "npx",
echo       "args": ["-y", "@upshift-inc/skill-seeker"]
echo     },
echo     "spec-workflow": {
echo       "command": "npx",
echo       "args": ["-y", "@upshift-inc/spec-workflow"]
echo     },
echo     "playwright-mcp": {
echo       "command": "npx",
echo       "args": ["-y", "@playwright/mcp"]
echo     }
echo   }
echo }
) > "%USERPROFILE%\.claude\claude_desktop_config.json"

if errorlevel 1 (
    echo ❌ 写入配置失败
    pause
    exit /b 1
)
echo ✓ 配置已写入: %USERPROFILE%\.claude\claude_desktop_config.json

REM 完成提示
echo.
echo === 配置完成 ===
echo.
echo 已配置的 MCP 服务:
echo   1. context7         - 获取库文档和上下文
echo   2. serena           - 语义代码分析工具
echo   3. skill-seeker     - 创建 Claude 自定义技能
echo   4. spec-workflow    - 功能开发工作流管理
echo   5. playwright-mcp   - 浏览器自动化测试
echo.
echo 下一步操作:
echo   1. 如需配置 Shadcn Vue MCP Server，请:
echo      - 手动安装到本地目录
echo      - 编辑配置文件添加路径
echo   2. 重启 Claude Code CLI
echo   3. 运行 'claude mcp list' 验证配置
echo   4. 测试服务: 'claude mcp test ^<服务名^>'
echo.
echo 配置文件位置: %USERPROFILE%\.claude\claude_desktop_config.json
echo 完整文档: docs\MCP服务配置完整指南.md
echo.
pause
